<import resource="classpath:alfresco/extension/templates/webscripts/vgr/common.js">

modelPrefix = "{vgr.model}";
filename = null;
content = null;

createFolders();
vgr = companyhome.childByNamePath("vgr");
publishedFolder = vgr.childByNamePath("published");

function handleField(field, system) {
	systemProperties = aspects.getAspectProperties(modelPrefix + system);
	
	if (systemProperties != null) {
		for each (property in systemProperties) {
			propertyName = property.toString();
		}
	}
}

// locate mandatory fields
for each (field in formdata.fields) {
	
	// mandatory fields
	if (field.name == "file" && field.isFile) {
		filename = field.filename;
		content = field.content;
	}
	
	// mandatory fields for publishable aspect 
	if (field.name == modelPrefix + "accessZone") { accessZone = field.value; }
	if (field.name == modelPrefix + "publishedAt") { publishedAt = field.value; }
	if (field.name == modelPrefix + "publishedBy") { publishedBy = field.value; }
	if (field.name == modelPrefix + "published") { published = field.value; }
	
	handleField(field, args.system);
	
	// mandatory fields for systemABC aspect
	if (args.system == "systemABC") {
		if (field.name == modelPrefix + "documentNumber") { documentNumber = field.value; }
		if (field.name == modelPrefix + "expireDate") { expireDate = field.value; }
		if (field.name == modelPrefix + "description") { description = field.value; }
	}
	
	// mandatory fields for systemXYZ aspect
	if (args.system == "systemXYZ") {
		if (field.name == modelPrefix + "identifier") { identifier = field.value; }
		if (field.name == modelPrefix + "author") { author = field.value; }
	}
	
	// mandatory fields for webbdok aspect
	if (args.system == "webbdok") {
		if (field.name == modelPrefix + "docType") { docType = field.value; }
		if (field.name == modelPrefix + "docTitle") { docTitle = field.value; }
		if (field.name == modelPrefix + "docId") { docId = field.value; }
		if (field.name == modelPrefix + "docCreatedBy") { docCreatedBy = field.value; }
		if (field.name == modelPrefix + "docDescription") { docDescription = field.value; }
		
		if (field.name == modelPrefix + "createdByAuthor") { createdByAuthor = field.value; }
		if (field.name == modelPrefix + "createdByAdministration") { createdByAdministration = field.value; }
		if (field.name == modelPrefix + "createdByUnit") { createdByUnit = field.value; }
		
		if (field.name == modelPrefix + "responsibleName") { responsibleName = field.value; }
		if (field.name == modelPrefix + "responsibleAdministration") { responsibleAdministration = field.value; }
		if (field.name == modelPrefix + "responsibleUnit") { responsibleUnit = field.value; }
		
		if (field.name == modelPrefix + "businessMajorArea") { businessMajorArea = field.value; }
		if (field.name == modelPrefix + "businessMinorArea") { businessMinorArea = field.value; }
		if (field.name == modelPrefix + "businessProject") { businessProject = field.value; }
		if (field.name == modelPrefix + "businessAssigner") { businessAssigner = field.value; }
		
		if (field.name == modelPrefix + "referenceDocumentSuite") { referenceDocumentSuite = field.value; }
		if (field.name == modelPrefix + "referenceNumber") { referenceNumber = field.value; }
		
		if (field.name == modelPrefix + "validFrom") { validFrom = field.value; }
		if (field.name == modelPrefix + "validTo") { validTo = field.value; }
		if (field.name == modelPrefix + "approvedBy") { approvedBy = field.value; }
		if (field.name == modelPrefix + "approvedTimestamp") { approvedTimestamp = field.value; }
		if (field.name == modelPrefix + "filename") { _filename = field.value; }
		if (field.name == modelPrefix + "documentUrl") { documentUrl = field.value; }
		
		if (field.name == modelPrefix + "oldVersion") { oldVersion = field.value; }
		if (field.name == modelPrefix + "documentUrl") { documentUrl = field.value; }
		if (field.name == modelPrefix + "docCreatedAt") { docCreatedAt= field.value; }
	}
}

// ensure mandatory file attributes have been located
if (filename == undefined || content == undefined) {
  status.code = 400;
  status.message = "Uploaded file cannot be located in request";
  status.redirect = true;
} else {

  // create document in vgr folder for uploaded file
  upload = vgr.createFile(filename);
  upload.specializeType("vgr:doc");
  
  // Add publishable aspect
  var publishableProps = new Array(5);
  publishableProps["vgr:accessZone"] = accessZone;
  publishableProps["vgr:publishedBy"] = publishedBy;
  publishableProps["vgr:publishedAt"] = Date.parse(publishedAt);
  publishableProps["vgr:published"] = published;
  upload.addAspect("vgr:publishable", publishableProps);
  
  // Add systemABC aspect
  if (args.system == "systemABC") {
	  var systemABCProps = new Array(3);
	  systemABCProps["vgr:documentNumber"] = documentNumber;
	  systemABCProps["vgr:expireDate"] = Date.parse(expireDate);
	  systemABCProps["vgr:description"] = description;
	  upload.addAspect("vgr:systemABC", systemABCProps);
  }
  
  // Add systemXYZ aspect
  if (args.system == "systemXYZ") {
	  var systemXYZProps = new Array(2);
	  systemXYZProps["vgr:identifier"] = identifier;
	  systemXYZProps["vgr:author"] = author;
	  upload.addAspect("vgr:systemXYZ", systemXYZProps);
  }
  
  // Add webbdok aspect
  if (args.system == "webbdok") {
	  var systemWebbdokProps = new Array(16);
	  systemWebbdokProps["vgr:docType"] = docType;
	  systemWebbdokProps["vgr:docTitle"] = docTitle;
	  systemWebbdokProps["vgr:docId"] = docId;
	  systemWebbdokProps["vgr:docCreatedBy"] = docCreatedBy;
	  systemWebbdokProps["vgr:docDescription"] = docDescription;
	  
	  systemWebbdokProps["vgr:createdByAuthor"] = createdByAuthor;
	  systemWebbdokProps["vgr:createdByAdministration"] = createdByAdministration;
	  systemWebbdokProps["vgr:createdByUnit"] = createdByUnit;
	  
	  systemWebbdokProps["vgr:responsibleName"] = responsibleName;
	  systemWebbdokProps["vgr:responsibleAdministration"] = responsibleAdministration;
	  systemWebbdokProps["vgr:responsibleUnit"] = responsibleUnit;
	  
	  systemWebbdokProps["vgr:businessMajorArea"] = businessMajorArea;
	  systemWebbdokProps["vgr:businessMinorArea"] = businessMinorArea;
	  systemWebbdokProps["vgr:businessProject"] = businessProject;
	  systemWebbdokProps["vgr:businessAssigner"] = businessAssigner;
	  
	  systemWebbdokProps["vgr:referenceDocumentSuite"] = referenceDocumentSuite;
	  systemWebbdokProps["vgr:referenceNumber"] = referenceNumber;
	  
	  systemWebbdokProps["vgr:validFrom"] = validFrom;
	  systemWebbdokProps["vgr:validTo"] = validTo;
	  systemWebbdokProps["vgr:approvedBy"] = approvedBy;
	  systemWebbdokProps["vgr:approvedTimestamp"] = approvedTimestamp;
	  systemWebbdokProps["vgr:filename"] = _filename;
	  systemWebbdokProps["vgr:documentUrl"] = documentUrl;
	  
	  systemWebbdokProps["vgr:oldVersion"] = oldVersion;
	  systemWebbdokProps["vgr:documentUrl"] = documentUrl;
	  systemWebbdokProps["vgr:docCreatedAt"] = docCreatedAt;
	  
	  upload.addAspect("vgr:webbdok", systemWebbdokProps);
  }
  
  upload.properties.content.write(content);
  upload.properties.encoding = "UTF-8";
  upload.save();
  
  //PDF transformation
  if (published == "true") {
	  pdfTransformation = upload.transformDocument("application/pdf", publishedFolder);
	  
	  if (pdfTransformation == null) {
		  status.code = 500;
		  status.message = "Could not transform document";
		  status.redirect = true;
	  } else {
		  upload.createAssociation(pdfTransformation, "vgr:pdfTransformation");
		  upload.save();
	  }
  }
  
  // setup model for response template
  model.upload = upload;
}